#include <iostream>
#include <string.h>
#include <fstream>
#include "jsoncpp/json/json.h"

using namespace std;

void ReadFileJson()
{
	Json::Value root; //定义根节点
	Json::Reader reader;
	ifstream in("/home/louislou/Codes/learn/louis_json_parser/json_cpp_contrast/test.json", ios::binary);//输入json文件的绝对路径
	if (!in.is_open())
	{
		cout << "文件打开错误"<<endl;
		return;
	}

	if (reader.parse(in, root))
	{
		// 普通键值解析
        string name = root["name"].asString();
		int no_camp = root["no_camp"].asInt();
		int save_state = root["save_state"].asInt();
		int state_locked = root["state_locked"].asInt();
		int default_state = root["default_state"].asInt();
		int recover_state = root["recover_state"].asInt();
		cout << "name             is  " << name << endl;
		cout << "no_camp          is  " << no_camp << endl;
		cout << "save_state       is  " << save_state << endl;
		cout << "state_locked     is  " << state_locked << endl;
		cout << "default_state    is  " << default_state << endl;
		cout << "recover_state    is  " << recover_state << endl;
		string relive_type = root["relive"]["type"].asString();
		int  relive_relive_item = root["relive"]["relive_item"].asInt();
		cout << "relive_type      is  " << relive_type << endl;
		cout << "relive_camp_item is  " << relive_relive_item << endl;
		int sz = root["relive"]["state"].size();
        // 简单数组解析
        for (int i = 0; i < sz; i++)
		{
			int state = root["relive"]["state"][i].asInt();
			cout<<"state is" << state;
		}
		cout <<  endl;
		int size = root["relive"]["camp_relive"].size();
        // 复杂数组解析
		for (int i = 0; i < size; i++)
		{
			int posx = root["relive"]["camp_relive"][i]["posX"].asInt();
			int posy = root["relive"]["camp_relive"][i]["posY"].asInt();
			cout << "posx is " << posx << ", posy is " << posy << endl;
		}
	}
}

int main() {
    ReadFileJson();
    return 0;
}